package com.example.network

import com.example.util.DigestUtils
import java.io.IOException
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object SendRequest {

    //公共参数
    private val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl("https://home.mifon.com/NetworkPlatform/rest/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    // 发送验证码
    fun sendVerCode(phoneNumber: String) {
        val testVerCode1: ITestVerCode = retrofit.create(ITestVerCode::class.java)

        val call: Call<ResponseTestVerBean> = testVerCode1.post(RequestSendVerCode("zh", "1", "86", phoneNumber))

        call.enqueue(object : Callback<ResponseTestVerBean> {
            override fun onResponse(call: Call<ResponseTestVerBean>, response: Response<ResponseTestVerBean>) {
                if (response.isSuccessful && response.body() != null) {
                    println(response.body()?.getResultDesc())
                } else {
                    println("请求失败，状态码: ${response.code()}")
                    if (response.errorBody() != null) {
                        try {
                            println("错误信息: ${response.errorBody()?.string()}")
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                }
            }

            override fun onFailure(call: Call<ResponseTestVerBean>, t: Throwable) {
                t.printStackTrace()
                println("网络错误: ${t.message}")
            }
        })
    }

    //验证验证码
    fun checkVerCode(phoneNumber: String, verCode: String, listener: CheckResultListener) {
        val api: ICheckVerCode = retrofit.create(ICheckVerCode::class.java)
        val call: Call<ResponseCheckVerBean> = api.checkVerificationCode("86", phoneNumber, verCode, "1")

        call.enqueue(object : Callback<ResponseCheckVerBean> {
            override fun onResponse(call: Call<ResponseCheckVerBean>, response: Response<ResponseCheckVerBean>) {
                if (response.isSuccessful && response.body() != null) {
                    //此处判断是否成功代理，若成功则返回true
                    listener.onSuccess(response.body()?.getResultDesc() ?: "")
                    println(response.body()?.getResultDesc())
                } else {
                    listener.onFail("状态码=${response.code()}")
                    println("请求失败，状态码: ${response.code()}")
                    if (response.errorBody() != null) {
                        try {
                            println("错误信息: ${response.errorBody()?.string()}")
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                }
            }

            override fun onFailure(call: Call<ResponseCheckVerBean>, t: Throwable) {
                println("网络错误: ${t.message}")
            }
        })
    }

    //开始注册 - 异步版本
    fun registerUser(phoneNumber: String, verCode: String, listener: CheckResultListenerAddresponse) {
        val iRegister: IRegister = retrofit.create(IRegister::class.java)
        val loginBeanCall: Call<ResponseRegisterBean> = iRegister.login(RequestRegisterBean(DigestUtils.encryptBySha256("1567day"), "86", phoneNumber, verCode))

        // 使用enqueue异步执行网络请求
        loginBeanCall.enqueue(object : Callback<ResponseRegisterBean> {
            override fun onResponse(call: Call<ResponseRegisterBean>, response: Response<ResponseRegisterBean>) {
                if (response.isSuccessful && response.body() != null) {
                    //此处判断是否成功代理，若成功则返回true
                    listener.onSuccess(response.body()!!)
                    println(response.body()?.getResultDesc())
                } else {
                    listener.onFail("状态码=${response.code()}")
                    println("请求失败，状态码: ${response.code()}")
                    if (response.errorBody() != null) {
                        try {
                            println("错误信息: ${response.errorBody()?.string()}")
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                }
            }

            override fun onFailure(call: Call<ResponseRegisterBean>, throwable: Throwable) {
                println("网络错误: ${throwable.message}")
            }
        })
    }

    //登录
    fun loginUser(phoneNumber: String, passWord: String, listener: CheckResultListenerAddresponseLogin) {
        val iLogin: ILogin = retrofit.create(ILogin::class.java)
        val loginBeanCall: Call<ResponseLoginBean> = iLogin.login(RequestLoginBean1(phoneNumber, DigestUtils.encryptBySha256(passWord), "86"))

        // 使用enqueue异步执行网络请求
        loginBeanCall.enqueue(object : Callback<ResponseLoginBean> {
            override fun onResponse(call: Call<ResponseLoginBean>, response: Response<ResponseLoginBean>) {
                if (response.isSuccessful && response.body() != null) {
                    //此处判断是否成功代理，若成功则返回true
                    listener.onSuccess(response.body()!!)
                    println(response.body()?.getResultDesc())
                } else {
                    listener.onFail("状态码=${response.code()}")
                    println("请求失败，状态码: ${response.code()}")
                    if (response.errorBody() != null) {
                        try {
                            println("错误信息: ${response.errorBody()?.string()}")
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                }
            }

            override fun onFailure(call: Call<ResponseLoginBean>, throwable: Throwable) {
                // 可以在这里添加失败回调
            }
        })
    }
}